package com.dhcc.bpm.modules.log.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.modules.api.service.IApiService;
import com.dhcc.bpm.modules.log.entity.ApiOperLog;
import com.dhcc.bpm.modules.log.service.IApiOperLogService;
import com.dhcc.bpm.modules.log.vo.ApiOperLogVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 控制器
 *
 * @author xiafayu
 * @since 2022-04-04
 */
@RestController
@AllArgsConstructor
@RequestMapping("cbus-bpm-boot/opcoperlog")
@Api(value = "", tags = "接口")
public class ApiOperLogController extends BladeController {

    private IApiOperLogService opcOperLogService;
    private IApiService apiService;

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入opcOperLog")
    public R<ApiOperLogVO> detail(ApiOperLog opcOperLog) {
        ApiOperLog detail = opcOperLogService.getOne(Condition.getQueryWrapper(opcOperLog));
        ApiOperLogVO apiOperLogVO = new ApiOperLogVO();
        BeanUtil.copyProperties(detail, apiOperLogVO);
        com.dhcc.bpm.modules.api.entity.Api api = apiService.getById(detail.getApiId());
        apiOperLogVO.setApiCode(api.getApiCode());
        apiOperLogVO.setApiName(api.getApiName());
        apiOperLogVO.setApiVersion(api.getApiVersion());
        return R.data(apiOperLogVO);
    }

    /**
     * 分页
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页", notes = "传入opcOperLog")
    public R<IPage<ApiOperLog>> list(ApiOperLog opcOperLog, Query query) {
        IPage<ApiOperLog> pages = opcOperLogService.page(Condition.getPage(query), Condition.getQueryWrapper(opcOperLog));
        return R.data(pages);
    }

    /**
     * 自定义分页
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页", notes = "传入opcOperLog")
    public R<IPage<ApiOperLogVO>> page(ApiOperLogVO opcOperLog, Query query) {
        if (opcOperLog.getOperDate() != null && opcOperLog.getOperDate().length() > 0) {
            String startDate = opcOperLog.getOperDate().split(",")[0];
            String endDate = opcOperLog.getOperDate().split(",")[1];
            opcOperLog.setStartDate(startDate);
            opcOperLog.setEndDate(endDate);
        }
        IPage<ApiOperLogVO> pages = opcOperLogService.selectOpcOperLogPage(Condition.getPage(query), opcOperLog);
        return R.data(pages);
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "传入opcOperLog")
    public R save(@Valid @RequestBody ApiOperLog opcOperLog) {
        return R.status(opcOperLogService.save(opcOperLog));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入opcOperLog")
    public R update(@Valid @RequestBody ApiOperLog opcOperLog) {
        return R.status(opcOperLogService.updateById(opcOperLog));
    }

    /**
     * 新增或修改
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增或修改", notes = "传入opcOperLog")
    public R submit(@Valid @RequestBody ApiOperLog opcOperLog) {
        return R.status(opcOperLogService.saveOrUpdate(opcOperLog));
    }


    /**
     * 删除
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "逻辑删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(opcOperLogService.deleteLogic(Func.toLongList(ids)));
    }


}
